package io.linfeng.modules.oss.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.linfeng.common.exception.LinfengException;
import io.linfeng.common.utils.PageUtils;
import io.linfeng.common.utils.Query;
import io.linfeng.config.StaticConfig;
import io.linfeng.modules.oss.dao.SysOssDao;
import io.linfeng.modules.oss.entity.SysOssEntity;
import io.linfeng.modules.oss.service.SysOssService;
import io.linfeng.modules.utils.Graphics2DInfo;
import io.linfeng.modules.utils.ImageUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.awt.*;
import java.io.File;
import java.util.*;
import java.util.List;


@Service("sysOssService")
public class SysOssServiceImpl extends ServiceImpl<SysOssDao, SysOssEntity> implements SysOssService {

	@Autowired
	StaticConfig staticConfig;


	@Value("${img.host}")
	String host;



	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		IPage<SysOssEntity> page = this.page(
			new Query<SysOssEntity>().getPage(params)
		);

		return new PageUtils(page);
	}
	@Override
	public String upload2(MultipartFile file) throws Exception {

		// 获取文件名
		String fileName = file.getOriginalFilename();
		// 获取文件的后缀名
		String suffixName = fileName.substring(fileName.lastIndexOf("."));
		List<String> extList = Arrays.asList(".jpg", ".png", ".jpeg", ".gif");
		if (!extList.contains(suffixName)) {
			log.error("非法图片格式为:"+suffixName);
			throw new LinfengException("图片格式非法");
		}
		// 解决中文问题，liunx下中文路径，图片显示问题
		fileName = UUID.randomUUID().toString().replace("-", "") + suffixName;
		// 返回客户端 文件地址 URL
		String url = staticConfig.getUploadPath() + fileName;
		File dest = new File( staticConfig.getUploadImgPath() + fileName);
		// 检测是否存在目录
		if (!dest.getParentFile().exists()) {
			dest.getParentFile().mkdirs();
		}
		try {
			file.transferTo(dest);
		}catch (Exception e){
			e.printStackTrace();
		}

		return host+url;
	}


	@Override
	public String upload(MultipartFile file) throws Exception {

		// 获取文件名
		String fileName = file.getOriginalFilename();
		// 获取文件的后缀名
		String suffixName = fileName.substring(fileName.lastIndexOf("."));
		List<String> extList = Arrays.asList(".jpg", ".png", ".jpeg", ".gif");
		if (!extList.contains(suffixName)) {
			log.error("非法图片格式为:"+suffixName);
			throw new LinfengException("图片格式非法");
		}
		// 解决中文问题，liunx下中文路径，图片显示问题
		fileName = UUID.randomUUID().toString().replace("-", "") + suffixName;
		// 返回客户端 文件地址 URL
		String url = staticConfig.getUploadPath() + fileName;

		List<Graphics2DInfo> contentList = new ArrayList<>();
		contentList.add(new Graphics2DInfo()
				.setSize(20)
				.setColor(Color.WHITE)
				.setContent(DateUtil.format(new Date(), "HH:mm")));
//		contentList.add(new Graphics2DInfo()
//				.setSize(10)
//				.setColor(Color.white)
//				.setContent(DateUtil.format(new Date(), "yyyy-MM-dd") + " " + SecurityUtils.getLoginUser().getUser().getNickName()));
//		contentList.add(new Graphics2DInfo()
//				.setSize(10)
//				.setColor(Color.white)
//				.setContent(room.getName().replace("单元","")));
		float quality =0.2f;
		float res =	file.getSize()/1024;
		if(res<200){
			quality = 0.5f;
		}else if(res>400){
			quality = 0.15f;
		}
		ImageUtils.imgWaterPress(multipartFileToFile(file), staticConfig.getUploadImgPath() , fileName, contentList,quality);



		return host+url;
	}


	public static File multipartFileToFile(MultipartFile multiFile) {
		// 获取文件名
		String fileName = multiFile.getOriginalFilename();
		// 获取文件后缀
		String prefix = fileName.substring(fileName.lastIndexOf("."));
		// 若需要防止生成的临时文件重复,可以在文件名后添加随机码

		try {
			File file = File.createTempFile(fileName, prefix);
			multiFile.transferTo(file);
			return file;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
